package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.bean.AccountBean;

public class AccountDao {
private DBSource db = new SimpleDBSource();
	
	//添加
	public int addBank(AccountBean bean) {
		int rows = 0;
		Connection conn = null;
		try{
			conn = db.getConn();
			String sql = "insert into account(no,bank,money,sdate,user) values(?,?,?,?,?)";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, bean.getNo());
			ps.setString(2, bean.getBank());
			ps.setFloat(3, bean.getMoney());
			ps.setString(4, bean.getSdate());
			ps.setString(5, bean.getUser());
			rows = ps.executeUpdate();
			ps.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			db.closeConn(conn);
		}
		return rows;
	}

	//修改
	public int updateBank(AccountBean bean) {
		int rows = 0;
		Connection conn = null;
		try{
			conn = db.getConn();
			String sql = "update account set money = ? where no = ?";
			PreparedStatement ps = conn.prepareStatement(sql);
			
			ps.setFloat(1, bean.getMoney());
			ps.setString(2, bean.getNo());
			rows = ps.executeUpdate();
			ps.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			db.closeConn(conn);
		}
		return rows;
	}

	//删除
	public int deleteBank(String no) {
		int rows = 0;
		Connection conn = null;
		try{
			conn = db.getConn();
			String sql = "delete from account where no = ?";
			PreparedStatement ps = conn.prepareStatement(sql);
			
			
			ps.setString(1, no);
			rows = ps.executeUpdate();
			ps.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			db.closeConn(conn);
		}
		return rows;
	}
	
	//查询单个
	public AccountBean selectByno(String no) {
		Connection conn = null;
		AccountBean bean = new AccountBean();
		try{
			conn = db.getConn();
			String sql = "select * from account where no = "+no;
			PreparedStatement ps = conn.prepareStatement(sql);
			
			
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				
				bean.setNo(rs.getString("no"));
				bean.setBank(rs.getString("bank"));
				bean.setMoney(rs.getFloat("money"));
				bean.setSdate(rs.getString("sdate"));
				bean.setUser(rs.getString("user"));
				
			}
			rs.close();
			ps.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			db.closeConn(conn);
		}
		
		return bean;
	}
	
	
	//查询全部
	public List<AccountBean> selectAll() {
		Connection conn = null;
		List<AccountBean> accounts = new ArrayList<AccountBean>();
		try{
			conn = db.getConn();
			String sql = "select * from account ";
			PreparedStatement ps = conn.prepareStatement(sql);
			
	
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				AccountBean bean = new AccountBean();
				bean.setNo(rs.getString("no"));
				bean.setBank(rs.getString("bank"));
				bean.setMoney(rs.getFloat("money"));
				bean.setSdate(rs.getString("sdate"));
				bean.setUser(rs.getString("user"));
				accounts.add(bean);
			}
			rs.close();
			ps.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			db.closeConn(conn);
		}
		
		return accounts;
	}
	
	/**
	 * 修改银行卡信息
	 * @param no
	 * @param money
	 */
	public void AccoutUpdate(String no,Float money){
		Connection conn = null;
		PreparedStatement pstate = null;
		String sql = "update accout set money=? where no=?";
		try {
			conn = db.getConn();
			pstate = conn.prepareStatement(sql);
			pstate.setFloat(1, money);
			pstate.setString(2, no);
			pstate.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			db.closeConn(conn);
		}
	}
}
